package com.cool4j.generator.entity

import org.apache.commons.lang3.StringUtils

/**
 * 数据库表字段信息类
 *
 * @author Frank create on 16/1/7 10:33
 */
class Column {

    String columnName

    String fieldName

    String typeName

    String javaTypeName
    /* 字段长度 */
    int columnSize
    /* 字段注释 */
    String remark
    /* 字段类型名全称,用于构造 import */
    String fullType

    boolean isPrimary

    String isNullable

    String methodName

    /* Groovy 中不需要显示定义 getter/setter ,只需要写重载的 getter/setter */

    /**
     * @param columnName - 数据库字段名
     */
    void setColumnName(columnName) {
        this.columnName = columnName

        def words = StringUtils.split(columnName.toLowerCase(), "_")
        def sb = new StringBuffer()
        /* 将字段名转为驼峰样式 */
        // Groovy 闭包迭代
        words.each { word ->
            sb.append(StringUtils.capitalize(word))
        }
        this.methodName = sb.toString()
        this.fieldName = StringUtils.uncapitalize(sb.toString())  // 首字母小写
    }

    void setTypeName(typeName) {
        this.typeName = typeName
        switch (typeName) {
            case "BIGINT":
                this.javaTypeName = "Long"
                break
            case "BIGINT UNSIGNED":
                this.javaTypeName = "Long"
                break
            case "NUMBER":
                this.javaTypeName = "Integer"
                break
            case "INT":
                this.javaTypeName = "Integer"
                break
            case "TINYINT":
                this.javaTypeName = "Integer"
                break
            case "INTEGER":
                this.javaTypeName = "Integer"
                break
            case "FLOAT":
                this.javaTypeName = "Float"
                break
            case "DOUBLE":
                this.javaTypeName = "Double"
                break
            case "DECIMAL":
                this.javaTypeName = "BigDecimal"
                this.fullType = "java.math.BigDecimal"
                break
            case "DATE":
                this.javaTypeName = "Timestamp"
                this.fullType = "java.sql.Timestamp"
                break
            case "DATETIME":
                this.javaTypeName = "Timestamp"
                this.fullType = "java.sql.Timestamp"
                break
            case "TIMESTAMP":
                this.javaTypeName = "Timestamp"
                this.fullType = "java.sql.Timestamp"
                break
            case "TIME":
                this.javaTypeName = "Timestamp"
                this.fullType = "java.sql.Timestamp"
                break
            case "TEXT":
                this.javaTypeName = "String"
                break
            case "VARCHAR":
                this.javaTypeName = "String"
                break
            case "VARCHAR2":  // Oracle 特有属性
                this.javaTypeName = "String"
                break
            case "CHAR":
                this.javaTypeName = "String";
                break
            default:
                this.javaTypeName = typeName;
        }
    }

    String getRemark() {
        return remark == null ? "" : remark
    }

}
